package openbang.account.web.portal.widget;

import java.util.List;

import openbang.account.service.entity.MsgAsyn;
import openbang.account.service.exception.SecurityOBException;
import openbang.account.service.intf.AccountService;
import openbang.account.service.intf.MsgService;
import openbang.account.service.vo.ExceptionEnum;
import openbang.account.service.vo.MsgServiceEnum;
import openbang.common.vo.SelectModel;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;

/**
 * 发送消息
 * 1、发新消息
 * 2、回复消息
 * @author aibo zeng
 *
 */
public class SendMsgWidget extends Composite{
	//ROOT Panel
	Grid sendMsgGrid = new Grid(4,3);

	EmailTextBox receBox = new EmailTextBox(0);
	TextBox titleBox = new TextBox();
	TextArea contentArea = new TextArea();
	Button   sendBtn = new Button("send");
	
	SelectDialog selectEmailDialog = null;
	List<SelectModel> emailSelectModels = null;

	public SendMsgWidget(){
		initWidget(sendMsgGrid);
		sendMsgGrid.setStyleName("sendMsgTable");
		HTML receUser = new HTML("<a href=\"#\">收件人:</a>");
		sendMsgGrid.setWidget(0, 0,receUser );
		sendMsgGrid.setWidget(0, 1,receBox);
		sendMsgGrid.setHTML(0, 2,"填写email,多个使用分号分隔");
		sendMsgGrid.setHTML(1, 0, "标题:");
		sendMsgGrid.setWidget(1, 1,titleBox);
		sendMsgGrid.setHTML(2, 0, "内容:");
		sendMsgGrid.setWidget(2, 1, contentArea);
		sendMsgGrid.setWidget(3, 1, sendBtn);
		receBox.setSize("350px", "");
		titleBox.setSize("350px", "");
		contentArea.setSize("350px","200px");
		sendMsgGrid.getColumnFormatter().setStyleName(0,"sendMsgTableCol0");
		sendMsgGrid.getColumnFormatter().setStyleName(1,"sendMsgTableCol1");
		sendMsgGrid.getColumnFormatter().setStyleName(2,"sendMsgTableCol2");

		receUser.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				if(selectEmailDialog==null){
					initSelectDialog();
				}
				else{
					selectEmailDialog.show();
					selectEmailDialog.center();
				}
			}
		});
		sendBtn.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				if(receBox.invalidNum()>0){
					Window.alert("请录入有效的邮件地址");
					receBox.setFocus(true);
					return;
				}
				if(titleBox.getText()==null  ||titleBox.getText().trim().length()<=1){
					Window.alert("请录入标题");
					titleBox.setFocus(true);
					return;
				}
				if(contentArea.getText()==null  ||contentArea.getText().trim().length()<=0 ){
					Window.alert("请录入消息内容");
					contentArea.setFocus(true);
					return;
				}
				
				MsgAsyn msg = new MsgAsyn();
				msg.setMsgTitle(titleBox.getText());
				msg.setMsgBody(contentArea.getText());
				MsgService.Util.getInstance().sendMsg(msg,receBox.getArrValue(), new AsyncCallback<Integer>(){
					public void onFailure(Throwable caught) {
						if(caught instanceof SecurityOBException){
							SecurityOBException obe = (SecurityOBException)caught;
							Window.alert( ExceptionEnum.getDesc(obe.getCode()));
						}
						else{
							Window.alert(caught.getMessage());
						}
					}
					public void onSuccess(Integer result) {
						Window.alert( MsgServiceEnum.getDesc(result));
					}
				});
			}				
		});		
	}
	
	public void initSelectDialog(){
		AccountService.Util.getInstance().getAllEmail(new AsyncCallback<List<SelectModel>>() {
			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());
			}
			public void onSuccess(List<SelectModel> result) {
				 emailSelectModels = result;
				 selectEmailDialog = new SelectDialog("选择用户邮件",result);
				 selectEmailDialog.setConfirmClickHandler(new OpenBangClickHandler() {
					public void onClick() {
						String selectStr = "";
						for(SelectModel  model :  emailSelectModels){
							if(model.isSelected()){
								selectStr = selectStr + model.getValue()+";";
							}
						}
						if(selectStr.length()>2){
							receBox.setText(selectStr.substring(0, selectStr.length()-1));
						}
					}
				});
				 selectEmailDialog.show();
				 selectEmailDialog.center();
			}
		});
	}	
	
	/**
	 * 由使用者提供 缺省的消息内容
	 * @param oldMsg
	 */
	public void replyMsg(MsgAsyn oldMsg){
		receBox.setText(oldMsg.getSenderEmail());
		titleBox.setText("Re:"+oldMsg.getMsgTitle());
		String defaultBody ="\n\n\n\nOriginal Message-------\n";
		defaultBody = defaultBody + "sender:"+oldMsg.getSenderEmail()+"\n";
		defaultBody = defaultBody + "send time:"+oldMsg.getSendTime()+"\n";
		defaultBody = defaultBody + "send content:"+oldMsg.getMsgBody()+"\n";
		contentArea.setText(defaultBody);
	}
	
	/**
	 * 指定发送一个新消息
	 */
	public void sendNewMsg(){
		receBox.setText("");
		titleBox.setText("");
		contentArea.setText("");		
	}
}
